<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxDomainAuth;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class WxDomainAuthController extends AdminController
{
    public function appCenter(Request $request, Content $content)
    {
        $domain = _empty_default_($request->domain, '');
        if(_empty_($domain) || $domain == get_mini_domain()){
            global $__MINI_GLOBAL_PAYCODE_0__,$__MINI_GLOBAL_PAYCODE_1__,$__MINI_GLOBAL_PAYCODE_2__,
                   $__MINI_GLOBAL_PAYCODE_3__,$__MINI_GLOBAL_PAYCODE_4__,$__MINI_GLOBAL_PAYCODE_5__,
                   $__MINI_GLOBAL_PAYCODE_6__,$__MINI_GLOBAL_PAYCODE_7__,$__MINI_GLOBAL_PAYCODE_8__,
                   $__MINI_GLOBAL_PAYCODE_9__,$__MINI_GLOBAL_PAYCODE_10__,$__MINI_GLOBAL_PAYCODE_11__,
                   $__MINI_GLOBAL_PAYCODE_12__,$__MINI_GLOBAL_PAYCODE_13__,$__MINI_GLOBAL_PAYCODE_14__,
                   $__MINI_GLOBAL_PAYCODE_15__,$__MINI_GLOBAL_PAYCODE_16__,$__MINI_GLOBAL_PAYCODE_17__,
                   $__MINI_GLOBAL_PAYCODE_18__,$__MINI_GLOBAL_PAYCODE_19__,$__MINI_GLOBAL_PAYCODE_20__,
                   $__MINI_GLOBAL_PAYCODE_21__,$__MINI_GLOBAL_PAYCODE_22__,$__MINI_GLOBAL_PAYCODE_23__,
                   $__MINI_GLOBAL_PAYCODE_24__,$__MINI_GLOBAL_PAYCODE_25__,$__MINI_GLOBAL_PAYCODE_26__,
                   $__MINI_GLOBAL_PAYCODE_27__,$__MINI_GLOBAL_PAYCODE_28__,$__MINI_GLOBAL_PAYCODE_29__,
                   $__MINI_GLOBAL_PAYCODE_30__, $__MINI_GLOBAL_PAYCODE_31__,$__MINI_GLOBAL_PAYCODE_32__,
                   $__MINI_GLOBAL_PAYCODE_33__,$__MINI_GLOBAL_PAYCODE_34__,$__MINI_GLOBAL_PAYCODE_35__,
                   $__MINI_GLOBAL_PAYCODE_36__;
            $param = [
                'current_domain' => '',
                'paycode_0' => $__MINI_GLOBAL_PAYCODE_0__,
                'paycode_1' => $__MINI_GLOBAL_PAYCODE_1__,
                'paycode_2' => $__MINI_GLOBAL_PAYCODE_2__,
                'paycode_3' => $__MINI_GLOBAL_PAYCODE_3__,
                'paycode_4' => $__MINI_GLOBAL_PAYCODE_4__,
                'paycode_5' => $__MINI_GLOBAL_PAYCODE_5__,
                'paycode_6' => $__MINI_GLOBAL_PAYCODE_6__,
                'paycode_7' => $__MINI_GLOBAL_PAYCODE_7__,
                'paycode_8' => $__MINI_GLOBAL_PAYCODE_8__,
                'paycode_9' => $__MINI_GLOBAL_PAYCODE_9__,
                'paycode_10' => $__MINI_GLOBAL_PAYCODE_10__,
                'paycode_11' => $__MINI_GLOBAL_PAYCODE_11__,
                'paycode_12' => $__MINI_GLOBAL_PAYCODE_12__,
                'paycode_13' => $__MINI_GLOBAL_PAYCODE_13__,
                'paycode_14' => $__MINI_GLOBAL_PAYCODE_14__,
                'paycode_15' => $__MINI_GLOBAL_PAYCODE_15__,
                'paycode_16' => $__MINI_GLOBAL_PAYCODE_16__,
                'paycode_17' => $__MINI_GLOBAL_PAYCODE_17__,
                'paycode_18' => $__MINI_GLOBAL_PAYCODE_18__,
                'paycode_19' => $__MINI_GLOBAL_PAYCODE_19__,
                'paycode_20' => $__MINI_GLOBAL_PAYCODE_20__,
                'paycode_21' => $__MINI_GLOBAL_PAYCODE_21__,
                'paycode_22' => $__MINI_GLOBAL_PAYCODE_22__,
                'paycode_23' => $__MINI_GLOBAL_PAYCODE_23__,
                'paycode_24' => $__MINI_GLOBAL_PAYCODE_24__,
                'paycode_25' => $__MINI_GLOBAL_PAYCODE_25__,
                'paycode_26' => $__MINI_GLOBAL_PAYCODE_26__,
                'paycode_27' => $__MINI_GLOBAL_PAYCODE_27__,
                'paycode_28' => $__MINI_GLOBAL_PAYCODE_28__,
                'paycode_29' => $__MINI_GLOBAL_PAYCODE_29__,
                'paycode_30' => $__MINI_GLOBAL_PAYCODE_30__,
                'paycode_31' => $__MINI_GLOBAL_PAYCODE_31__,
                'paycode_32' => $__MINI_GLOBAL_PAYCODE_32__,
                'paycode_33' => $__MINI_GLOBAL_PAYCODE_33__,
                'paycode_34' => $__MINI_GLOBAL_PAYCODE_34__,
                'paycode_35' => $__MINI_GLOBAL_PAYCODE_35__,
                'paycode_36' => $__MINI_GLOBAL_PAYCODE_36__
            ];
        }else{
            $resp = Http::withHeaders([
                'device' => 'pc'
            ])->post('https://mini.minisns.cn/api/v1/aggregate/user/hanlder', ['domain'=>$domain, 'action'=>'authorization']);
            if($resp->successful()){
                $res = $resp->json();
                if($res['status']){
                    $pays = trim(_array_key($res['data'], 'pays', ''), ',');
                    $pays = explode(',', $pays);
                    $param = [];
                    $param['current_domain'] = $domain;
                    for ($i = 0; $i <= 35; $i++){
                        $param['paycode_'.$i] = in_array($i, $pays);
                    }
                }else{
                    $param = [];
                    $param['current_domain'] = $domain;
                    for ($i = 0; $i <= 35; $i++){
                        $param['paycode_'.$i] = false;
                    }
                }
            }
        }

        return $content
            ->title('域名授权')
            ->description('自助授权，模块购买，代理人购买 系统中心')
            ->body(admin_view('admin.pages.app-center', $param));
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxDomainAuth(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'user_id','domain_1','domain_2', 'domain_3', 'who'])->placeholder('搜索ID，用户ID，域名，代理人...')->width(50);

            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');

            $grid->column('domain_1')->editable();
            $grid->column('tenant_1')->editable();
            $grid->column('domain_2')->editable();
            $grid->column('tenant_2')->editable();
            $grid->column('domain_3')->editable();
            $grid->column('tenant_3')->editable();
            $grid->column('domain_4')->editable();
            $grid->column('tenant_4')->editable();
            $grid->column('domain_5')->editable();
            $grid->column('tenant_5')->editable();
            $grid->column('domain_6')->editable();
            $grid->column('tenant_6')->editable();
            $grid->column('domain_7')->editable();
            $grid->column('tenant_7')->editable();
            $grid->column('pays')->editable();
            $grid->column('who');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxDomainAuth(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('domain_1');
            $show->field('tenant_1');
            $show->field('domain_2');
            $show->field('tenant_2');
            $show->field('domain_3');
            $show->field('tenant_3');
            $show->field('domain_4');
            $show->field('tenant_4');
            $show->field('domain_5');
            $show->field('tenant_5');
            $show->field('domain_6');
            $show->field('tenant_6');
            $show->field('domain_7');
            $show->field('tenant_7');
            $show->field('pays');
            $show->field('who');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxDomainAuth(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('domain_1');
            $form->text('tenant_1')->default('0,999');
            $form->text('domain_2');
            $form->text('tenant_2')->default('0,999');
            $form->text('domain_3');
            $form->text('tenant_3');
            $form->text('domain_4');
            $form->text('tenant_4');
            $form->text('domain_5');
            $form->text('tenant_5');
            $form->text('domain_6');
            $form->text('tenant_6');
            $form->text('domain_7');
            $form->text('tenant_7');
            $form->text('pays')->default(0);
            $form->text('who');

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
